from contextlib import asynccontextmanager
from typing import AsyncGenerator

from sqlalchemy.ext.asyncio import AsyncSession 
from ..db.database import SessionLocal 


async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
    db_session = None 
    try:
        db_session = SessionLocal()
        yield db_session 
    finally: 
        if not db_session is None: 
            await db_session.close()



@asynccontextmanager 
async def get_db_session_asynccont() -> AsyncGenerator: 
    async_session = SessionLocal()
    
    try: 
        yield async_session 
        await async_session.commit()

    except Exception as ex: 
        await async_session.rollback()
        raise ex 
    
    finally: 
        await async_session.close()


